package com.msstudio.midlets;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
import com.msstudio.utils.FriendRecord;
import com.msstudio.utils.PkgTools;
import com.msstudio.utils.HttpPoster;
import com.msstudio.utils.HttpPosterListener;
import com.msstudio.i18n.MultiLan;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import com.msstudio.*;
import com.msstudio.message.MessageSender;

public class MSCMIDlet extends MIDlet
        implements CommandListener, HttpPosterListener, ItemStateListener {

    private final static int MAX_USERID_LEN = 16;
    private final static int MAX_PASSWORD_LEN = 16;
    public static MSCMIDlet instance = null;
    public static Display display = null;
    public static Runtime runtime = null;
    private Form MessageBox;
    private Form InputForm;
    protected Command cmdLogin;
    protected Command cmdRegister;
    protected Command cmdBack;
    protected Command cmdExit;
    protected Command cmdAgain;
    protected Command cmdBackToHelp;
    protected Command cmdStart;
    protected Command cmdHelp;
    private Gauge theGauge;
    public int gaugeState;
    public String strUserID;
    public String strPasswd;
    private TextField fldUserID;
    private TextField fldPasswd;
    private TextField fldConfirmPasswd;
    private boolean bMaskable;
    private String strTruePasswd;
    private String oldOwner;
    public static String url = "http://localhost:8787";//"http://218.18.95.203:21001/";
    public static MainMenu mainMenu;
    public static HttpPoster httpPoster;  // Todo: httpPoster要被废弃
    public static MessageSender messageSender;    
    public static FriendRecord friendRecord;
    public static FriendList friendList;
    public static GetMsgDlg getMsgDlg;
    public static SendMsgDlg sendMsgDlg;
    public static AddFriend addFriend;
    public static Finger finger;
    public static AckSvrMsg ackSvrMsg;
    public static MainForm mainForm;//���������
    public static BlackList blackList;
    public static MultiLan multiLang;   //国际化支持
    public static HistoryMsg historyMsg;
    private boolean isPause;
    private boolean isLoginPaused;
    private Displayable dispPause;
    public boolean isWaitLogin;
    int loadCnt;
    private int logoTimer;
    private int timercnt;
    private Alert qqalr;
    private Displayable alrpre;

    public MSCMIDlet() {
        MessageBox = null;
        InputForm = null;
        cmdLogin = null;
        cmdBack = null;
        cmdExit = null;
        cmdAgain = null;
        cmdBackToHelp = null;
        cmdStart = null;
        cmdHelp = null;
        theGauge = null;
        gaugeState = 0;
        strUserID = null;
        strPasswd = null;
        fldUserID = null;
        fldPasswd = null;
        bMaskable = false;
        strTruePasswd = null;
        oldOwner = null;
        isPause = false;
        isLoginPaused = false;
        dispPause = null;
        isWaitLogin = false;
        loadCnt = 0;
        logoTimer = 0;
        timercnt = 0;
        qqalr = null;
        alrpre = null;
        instance = this;
        display = Display.getDisplay(this);
        multiLang = new MultiLan();
        httpPoster = new HttpPoster();
        messageSender = new MessageSender();
        
        historyMsg = new HistoryMsg();
        mainMenu = new MainMenu();
        friendRecord = new FriendRecord();
        friendList = new FriendList();
        mainForm = new MainForm();
        getMsgDlg = new GetMsgDlg();
        sendMsgDlg = new SendMsgDlg();
        addFriend = new AddFriend();
        finger = new Finger();
        ackSvrMsg = new AckSvrMsg();
        blackList = new BlackList();
    }

    public void init() {
        addFriend.init();
        friendList.init();
        getMsgDlg.init();
        historyMsg.init();
        mainForm.init();
        mainMenu.init();
        sendMsgDlg.init();
    }

    public void startApp() {
        if (isPause) {
            isPause = false;
            if (isLoginPaused && !isWaitLogin) {
                mainForm.show();
            } else {
                display.setCurrent(dispPause);
            }
        } else {
            javax.microedition.lcdui.Image image = PkgTools.createImage("/qq.png");//Logo
            ImageItem imageitem = new ImageItem(null, image, 3, null);
            FullScreen fullscreen = new FullScreen(image);
            display.setCurrent(fullscreen);// Logo
            multiLang.init();//  初始化国际化支持，根据locale设定显示内容：简体/繁体/英文       
            cmdLogin = new Command(multiLang.sLogin, Command.SCREEN, 1);
            cmdRegister = new Command(multiLang.sRegister, Command.SCREEN, 1);
            cmdBack = new Command(multiLang.sBack, Command.BACK, 2);
            cmdExit = new Command(multiLang.sExit, Command.EXIT, 2);
            cmdAgain = new Command(multiLang.sReLogin, 1, 1);
            cmdBackToHelp = new Command(multiLang.sBack, 2, 2);
            cmdStart = new Command(multiLang.sStart, 1, 1);
            cmdHelp = new Command(multiLang.sHelp, Command.HELP, 2);
            createLoginForm();
            multiLang.init2();
            mainForm.startSysTimer();
        }
    }

    public void pauseApp() {//��ͣ
        isPause = true;
        if (isWaitLogin) {
            isLoginPaused = true;
        } else {
            isLoginPaused = false;
            dispPause = getCurrent();
        }
    }

    public void destroyApp(boolean flag) {
    }

    public void quitApp() {
        mainForm.stopSysTimer();
        messageSender.abort();
        httpPoster.abort();
        destroyApp(false);
        notifyDestroyed();
    }

    public void onLoadEnd() {
        loadCnt++;
        if (loadCnt > 1 && isWaitLogin) {
            isWaitLogin = false;
            mainForm.show();
            stopTimer();
        }
    }

    protected void createLoginForm() {
        String userID = null;
        String password = null;
        bMaskable = false;
        if (strUserID != null && strPasswd != null) {
            userID = strUserID;
            password = strPasswd;
        } else {
            PkgTools.loadDB();
            oldOwner = PkgTools.getUIN();
            userID = oldOwner;
            password = PkgTools.getPassWord();
        }
        if (userID == null || password == null) {
            userID = new String("");
            password = new String("");
        } else if (userID.equals("0")) {
            userID = new String("");
            password = new String("");
            bMaskable = false;
        } else {
            strTruePasswd = password;
            password = new String("******");
            bMaskable = true;
        }
        fldUserID = new TextField(multiLang.sUserID, userID, MAX_USERID_LEN, TextField.ANY);
        fldPasswd = new TextField(multiLang.sPassword, password, MAX_PASSWORD_LEN, TextField.ANY);
        InputForm = new Form(multiLang.sLoginForm);
        InputForm.append(fldUserID);
        InputForm.append(fldPasswd);
        InputForm.addCommand(cmdLogin);
        InputForm.addCommand(cmdRegister);
        InputForm.addCommand(cmdHelp);
        InputForm.addCommand(cmdExit);
        InputForm.setCommandListener(this);
        InputForm.setItemStateListener(this);
        showLoginForm();
    }

    protected void showLoginForm() {
        isWaitLogin = false;
        //httpPoster.stopTimer();
        display.setCurrent(InputForm);
    }

    protected void createRegisterForm() {
        fldUserID = new TextField(multiLang.sUserID, "", MAX_USERID_LEN, TextField.ANY);
        fldPasswd = new TextField(multiLang.sPassword, "", MAX_PASSWORD_LEN, TextField.ANY);
        fldConfirmPasswd = new TextField(multiLang.sPassword, "", MAX_PASSWORD_LEN, TextField.ANY);
        InputForm = new Form(multiLang.sRegisterForm);
        InputForm.append(fldUserID);
        InputForm.append(fldPasswd);
        InputForm.addCommand(cmdRegister);
        InputForm.addCommand(cmdLogin);
        InputForm.addCommand(cmdHelp);
        InputForm.addCommand(cmdExit);
        InputForm.setCommandListener(this);
        InputForm.setItemStateListener(this);
        display.setCurrent(InputForm);
    }

    public void startTimer(int i) {
        timercnt = 0;
        logoTimer = i;
    }

    public void stopTimer() {
        logoTimer = 0;
    }

    /*
     * 这个函数被MainForm的线程run()调用，模拟一个定时器行为
     */
    public void OnTimer() {   
        switch (logoTimer) {
            case 0: 
            default:
                break;
            case 1: 
                timercnt++;
                if (timercnt == 6) {
                    stopTimer();
                }
                break;
            case 2: 
                timercnt++;
                if (gaugeState < 1) {
                    if (timercnt > 16) {
                        timercnt = 16;
                    }
                } else if (timercnt >= 38) {
                    timercnt = 38;
                }
                theGauge.setValue((timercnt / 2) * 5);
                break;
        }
    }

    public void onNetError(boolean flag) {//�������ʱ��
        httpPoster.stopTimer();
        mainForm.stopTimer();
        getMsgDlg.stopTimer();
        friendRecord.stopTimer();
        if (flag) {
            if (!isWaitLogin) {
                showMessage(multiLang.sNetError, multiLang.sNetErrorMsg, this, cmdAgain, cmdExit);
            }
        } else {
            showMessage(multiLang.sNetError, multiLang.sNetErrorMsg, this, cmdExit, null);
        }
    }

    public void showMessage(String s, String s1, CommandListener commandlistener, Command command, Command command1) {
        MessageBox = new Form(s);
        MessageBox.append(s1);
        if (command != null) {
            MessageBox.addCommand(command);
        }
        if (command1 != null) {
            MessageBox.addCommand(command1);
        }
        MessageBox.setCommandListener(commandlistener);
        display.setCurrent(MessageBox);
    }

    public void playAlert() {
        try {
            AlertType.CONFIRMATION.playSound(display);
        } catch (Exception exception) {
        }
    }

    public Displayable getCurrent() {
        Displayable displayable = display.getCurrent();
        if (displayable == qqalr) {
            return alrpre;
        } else {
            return displayable;
        }
    }

    public void showAlert(String s, String s1, Displayable displayable) {
        try {
            qqalr = new Alert(s, s1, null, null);
            qqalr.setType(AlertType.ERROR);
            if (displayable != null) {
                alrpre = displayable;
            } else {
                alrpre = getCurrent();
            }
            qqalr.setTimeout(2000);
            display.setCurrent(qqalr, alrpre);
        } catch (Exception exception) {
        }
    }

    public void showHelpForm() {
        showMessage(multiLang.sHelpForm, multiLang.sHelpMsg, this, cmdBack, null);
    }

    private void login() {
        //httpPoster.startTimer();
        strUserID = fldUserID.getString();
        strPasswd = fldPasswd.getString();
        try {
            if (strUserID.length() < 5) {
                showAlert(multiLang.sLoginFall, multiLang.sInvalidUid, InputForm);
                return;
            }
        } catch (Exception exception) {
            showAlert(multiLang.sLoginFall, multiLang.sInvalidUid, InputForm);
            return;
        }
        if (bMaskable && strPasswd.equals("******")) {
            strPasswd = strTruePasswd;
        }
        isWaitLogin = true;
        showMessage(multiLang.sMSC, multiLang.sLogining, this, null, cmdBack);
        theGauge = new Gauge("", false, 100, 0);
        MessageBox.append(theGauge);
        startTimer(2);
        theGauge.setValue(0);
        gaugeState = 0;
        //httpPoster.login(strUserID, strPasswd, this);
        messageSender.sendLoginMessage(strUserID, strPasswd);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == cmdLogin) {
            login();
            return;
        } 
        if (command == cmdBack) {
            showLoginForm();
            return;
        }
        if (command == cmdAgain) {
            showLoginForm();
            return;
        }
        if (command == cmdExit) {
            quitApp();
            return;
        } 
        if (command == cmdStart) {
            createLoginForm();
            return;
        } 
        if (command == cmdHelp || command == cmdBackToHelp) {
            showHelpForm();
            return;
        }
        return;
    }

    public void receiveHttpResponse(String s) {
        if (!isWaitLogin) {
            return;
        }
        String s1 = PkgTools.GetParaVal("CMD", s);
        String s2 = PkgTools.GetParaVal("RES", s);
        String s3 = PkgTools.GetParaVal("RS", s);
        String s4 = PkgTools.GetParaVal("RA", s);
        s1 = s1.toUpperCase();
        if (s1.equals("LOGIN") && s2.equals("0") && s3.equals("0")) {
            if (!mainForm.iFirstTimeQuery) {
                httpPoster.getFriend(mainForm);
                mainForm.startTimer();
                mainForm.show();
                return;
            }
            gaugeState = 1;
            httpPoster.sendList(mainForm);
            if (strUserID.equals(oldOwner)) {
                if (PkgTools.getControl().equals("0")) {
                    getMsgDlg.switchControl();
                }
                blackList.loadList();
            } else {
                PkgTools.setOtherInfo(strUserID, "1", blackList);
            }
            PkgTools.setUserInfo(strUserID, strPasswd);
        } else if (s2.equals("5")) {
            showAlert(multiLang.sLoginFall, multiLang.sInvalidUid, InputForm);
        } else if (s2.equals("0")) {
            showAlert(multiLang.sLoginFall, s4, InputForm);
        } else {
            showAlert(multiLang.sLoginFall, multiLang.sLoginFallMsg, InputForm);
        }
    }

    public void handleHttpError(String s) {
        if (s == null && getCurrent() != MessageBox) {
            return;
        } else {
            showAlert(multiLang.sLoginFall, multiLang.sNetConnectFallMsg, InputForm);
            return;
        }
    }

    public void itemStateChanged(Item item) {
        if (item == fldPasswd) {
            bMaskable = false;
        }
    }
}
